﻿using GalaSoft.MvvmLight.Messaging;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace TransControlSystem.View
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            #region 全屏效果代码
            WindowState = System.Windows.WindowState.Maximized;
            WindowStyle = System.Windows.WindowStyle.None; //仅工作区可见，不显示标题栏和边框
            ResizeMode = System.Windows.ResizeMode.CanResizeWithGrip;//不显示最大化和最小化按钮
            Topmost = false;    //窗口在最前
            Left = 0.0;
            Top = 0.0;
            Width = System.Windows.SystemParameters.PrimaryScreenWidth;
            Height = System.Windows.SystemParameters.PrimaryScreenHeight;
            #endregion
            Messenger.Default.Register<NotificationMessage<string>>(this, (nm) =>
            {
                if (nm.Notification == "ExecAnimation")
                {
                    Application.Current.Dispatcher.Invoke((Action)delegate ()
                    {
                        FeedingTruckMove(nm.Content);
                    });

                }
            });
        }
        /// <summary>
        /// 当前点
        /// </summary>
        private Point CurrentPoint = new Point(0, 0);
        /// <summary>
        /// 移动动画执行
        /// </summary>
        /// <param name="EndPoint">终点</param>
        private void FeedingTruckMove(string EndPoint)
        {
            Application.Current.Dispatcher.Invoke((Action)delegate ()
            {
                if (EndPoint != "")
                {
                    //双精度动画移动的属性
                    DoubleAnimation doubleAnimation = new DoubleAnimation();
                    //5秒内播放完动画
                    doubleAnimation.Duration = new Duration
                        (TimeSpan.FromSeconds(5));
                    //声明平移变换对象
                    TranslateTransform transform = new TranslateTransform();
                    //将上方的平移变换对象赋给加料车控件
                    FeedingTruck.RenderTransform = transform;
                    //将平移初始值设置为0
                    transform.X = CurrentPoint.X;
                    //获取轨道计算窗体
                    Window TrackWindow = Window.GetWindow(Track);
                    //依据轨道计算窗体的XY轴0坐标的位置开始测算当前轨道的左上角XY轴的位置
                    Point TrackPoint = Track.TransformToAncestor(TrackWindow).Transform(new Point(0, 0));
                    //通过获取目标点的X轴坐标减去轨道的X轴坐标得到X轴需要移动的距离
                    doubleAnimation.To = GetTargetPointX(EndPoint) - TrackPoint.X;
                    //根据X轴开始移动，移动距离来自上方计算得来的值
                    transform.BeginAnimation(TranslateTransform.XProperty, doubleAnimation);
                    CurrentPoint = new Point(double.Parse(doubleAnimation.To.ToString()), TrackPoint.Y);
                }

            });
        }

        /// <summary>
        /// 根据终点描述获取终点控件X轴坐标
        /// </summary>
        /// <param name="EndPoint"></param>
        /// <returns></returns>
        private Double GetTargetPointX(string EndPoint)
        {
            if (EndPoint == "1#隔条仓")
            {
                return GetTargetPointX(btn_SpacerStripWarehouseNo1);
            }
            else if (EndPoint == "2#隔条仓")
            {
                return GetTargetPointX(btn_SpacerStripWarehouseNo2);
            }
            else if (EndPoint == "1#堆垛位")
            {
                return GetTargetPointX(btn_StackingPositionNo1);
            }
            else if (EndPoint == "2#堆垛位")
            {
                return GetTargetPointX(btn_StackingPositionNo2);
            }
            else if (EndPoint == "底部隔条仓")
            {
                return GetTargetPointX(btn_BottomSpacerStripWarehouse);
            }
            else if (EndPoint == "隔条总仓")
            {
                return GetTargetPointX(btn_SpacerStripGeneralWarehouse);
            }
            else if (EndPoint == "1#均质炉")
            {
                return GetTargetPointX(btn_HeatingNo1);
            }
            else if (EndPoint == "2#均质炉")
            {
                return GetTargetPointX(btn_HeatingNo2);
            }
            else if (EndPoint == "1#冷却室")
            {
                return GetTargetPointX(btn_CoolingChamberNo1);
            }
            else if (EndPoint == "3#均质炉")
            {
                return GetTargetPointX(btn_HeatingNo3);
            }
            else if (EndPoint == "4#均质炉")
            {
                return GetTargetPointX(btn_HeatingNo4);
            }
            else if (EndPoint == "2#冷却室")
            {
                return GetTargetPointX(btn_CoolingChamberNo2);
            }
            else if (EndPoint == "1#解垛位")
            {
                return GetTargetPointX(btn_DestackingNo1);
            }
            else if (EndPoint == "2#解垛位")
            {
                return GetTargetPointX(btn_DestackingNo2);
            }
            else if (EndPoint == "3#解垛位")
            {
                return GetTargetPointX(btn_DestackingNo3);
            }
            else if (EndPoint == "3#隔条仓")
            {
                return GetTargetPointX(btn_SpacerStripWarehouseNo3);
            }
            return 0d;
        }


        /// <summary>
        /// 获取目标X轴坐标
        /// </summary>
        /// <param name="Target">目标</param>
        /// <returns>X轴坐标</returns>
        private double GetTargetPointX(FrameworkElement Target)
        {
            Window bor_SecondRegionWindow = Window.GetWindow(bor_SecondRegion);
            Point bor_SecondRegionPoint = bor_SecondRegionWindow.TransformToAncestor(bor_SecondRegionWindow).Transform(new Point(0, 0));
            Window TargetWindow = Window.GetWindow(Target);
            Point TargetPoint = Target.TransformToAncestor(TargetWindow).Transform(bor_SecondRegionPoint);
            return TargetPoint.X;
        }

        /// <summary>
        /// 自动生成列时间 隐藏掉不像显示的列
        /// </summary>
        /// <param name="sender">datagrid</param>
        /// <param name="e">事件</param>
        private void dg_taskList_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            if (e.Column.Header.ToString() == "Id" ||
                     e.Column.Header.ToString() == "Priority" ||
                     e.Column.Header.ToString() == "PriorityNo" ||
                     e.Column.Header.ToString() == "EquipmentName" ||
                     e.Column.Header.ToString() == "PriorityArrangementNumber" ||
                     e.Column.Header.ToString() == "OrderNo" ||
                     e.Column.Header.ToString() == "TagDesc" ||
                     e.Column.Header.ToString() == "Address" ||
                     e.Column.Header.ToString() == "ValueType" ||
                     e.Column.Header.ToString() == "Value" ||
                     e.Column.Header.ToString() == "ReadOrWrite" ||
                     e.Column.Header.ToString() == "Remark" ||
                     e.Column.Header.ToString() == "IsEnable" ||
                     e.Column.Header.ToString() == "Type" ||
                     e.Column.Header.ToString() == "IsTheTaskInProgress" ||
                     e.Column.Header.ToString() == "IsTheTaskNotPerformed" ||
                     e.Column.Header.ToString() == "FirstTriggersBuildTaskTagDesc" ||
                     e.Column.Header.ToString() == "FirstTriggersBuildTaskAddress" ||
                     e.Column.Header.ToString() == "FirstTriggersBuildTaskValueType" ||
                     e.Column.Header.ToString() == "FirstTriggersBuildTaskValue" ||
                     e.Column.Header.ToString() == "FirstTriggersBuildTaskPoint" ||
                     e.Column.Header.ToString() == "FirstTriggersBuildTaskValue" ||
                     e.Column.Header.ToString() == "SecondaryTriggersBuildTaskTagDesc" ||
                     e.Column.Header.ToString() == "SecondaryTriggersBuildTaskAddress" ||
                     e.Column.Header.ToString() == "SecondaryTriggersBuildTaskValueType" ||
                     e.Column.Header.ToString() == "SecondaryTriggersBuildTaskValue" ||
                     e.Column.Header.ToString() == "SecondaryTriggersBuildTaskPoint" ||
                     e.Column.Header.ToString() == "TriggeredTaskTagDesc" ||
                     e.Column.Header.ToString() == "TriggeredTaskAddress" ||
                     e.Column.Header.ToString() == "TriggeredTaskValueType" ||
                     e.Column.Header.ToString() == "TriggeredTaskValue" ||
                     e.Column.Header.ToString() == "TagIsOn" ||
                     e.Column.Header.ToString() == "FirstTriggersTagIsOn" ||
                     e.Column.Header.ToString() == "SecondaryTriggersTagIsOn" ||
                     e.Column.Header.ToString() == "TaskDesc" ||
                     e.Column.Header.ToString() == "CreateTime" ||
                     e.Column.Header.ToString() == "CreateTimeText" ||
                     e.Column.Header.ToString() == "StartTime" ||
                     e.Column.Header.ToString() == "StartTimeText" ||
                     e.Column.Header.ToString() == "StartTimeText" ||
                     e.Column.Header.ToString() == "EndTime" ||
                     e.Column.Header.ToString() == "EndTimeText" ||
                     e.Column.Header.ToString() == "StartPoint" ||
                     e.Column.Header.ToString() == "EndPoint" ||
                     e.Column.Header.ToString() == "Status" ||
                     e.Column.Header.ToString() == "DataOrderNo" ||
                     e.Column.Header.ToString() == "IsDelete" ||
                     e.Column.Header.ToString() == "Point" ||
                     e.Column.Header.ToString() == "IsTheTaskInProgressVisibility" ||
                     e.Column.Header.ToString() == "IsTheTaskNotPerformedVisibility" ||
                     e.Column.Header.ToString() == "FunctionSimulationVisibility" ||
                     e.Column.Header.ToString() == "IsDelete" ||
                     e.Column.Header.ToString() == "IsDeleteText"
               )
            {
                e.Cancel = true;
            }
        }
        /// <summary>
        /// 本窗体关闭时退出整个程序
        /// </summary>
        /// <param name="sender">本窗体</param>
        /// <param name="e">事件</param>
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            System.Environment.Exit(0);
        }
        /// <summary>
        /// 任务列表加载列事件
        /// </summary>
        /// <param name="sender">窗体</param>
        /// <param name="e">事件</param>
        private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }
    }
}
